package com.zhuoyue.web.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.ApiController;

import com.zhuoyue.biz.core.api.dao.TblPasswordShareMapper;
import com.zhuoyue.biz.core.api.domain.TblPasswordShare;
import com.zhuoyue.biz.core.api.dto.rep.LockPasswordShareDTO;
import com.zhuoyue.biz.core.api.dto.rep.TblPasswordShareDTO;
import com.zhuoyue.common.dto.ControllerResult;
import com.zhuoyue.common.utils.AesUtils;
import com.zhuoyue.common.utils.Time;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * (TblPasswordShare)表控制层
 * 临时密码查询接口
 *
 * @author hewenze
 * @since 2021-01-26 11:05:13
 */
@RestController
@RequestMapping("/api/password/share")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TblPasswordShareController extends ApiController {

    private final TblPasswordShareMapper tblPasswordShareMapper;

    /**
     * 根据deviceMac查询密码信息
     *
     * @param deviceMac
     * @return
     */
    @GetMapping("/message")
    public ControllerResult lock(String deviceMac, String type) {

        LockPasswordShareDTO passwordShare = tblPasswordShareMapper.getPasswordShareByDeviceMac(deviceMac, type);
        if (passwordShare == null) {
            return ControllerResult.toNoDataFail("该设备暂无密码");
        } else {
            //将省-市-区转换为省市区
            if (StringUtils.isNotEmpty((passwordShare.getLocation()))) {
                passwordShare.setLocation(passwordShare.getLocation().replaceAll("-", ""));
            } else {
                passwordShare.setLocation("暂无区域信息");
                passwordShare.setAddress("暂无位置信息");
            }
        }

        return ControllerResult.toReturnDataSuccess("", passwordShare);
    }

    /**
     * 用于微信分享页面数据查询，id经过加密，需解密
     * 根据id查询密码信息（H5页面内进行请求)
     *
     * @param id
     * @return
     */
    @GetMapping("/id")
    public ControllerResult lockEncrypt(String id) {
        //参数字符串转义，将字符中的‘+’进行处理
        id = id.replaceAll(" +", "+");
        String decryptId = AesUtils.decrypt(id);
        System.out.println("解密过后的id" + decryptId);
        LockPasswordShareDTO passwordShare = tblPasswordShareMapper.getPasswordShareById(decryptId);
        if (passwordShare == null) {
            System.out.println("没有密码");
        } else {
            passwordShare.setLockPassword(AesUtils.decrypt(passwordShare.getLockPassword()));
            //将省-市-区转换为省市区
            if (StringUtils.isNotEmpty((passwordShare.getLocation()))) {
                passwordShare.setLocation(passwordShare.getLocation().replaceAll("-", ""));
            } else {
                passwordShare.setLocation("暂无区域信息");
                passwordShare.setAddress("暂无位置信息");
            }

            System.out.println("密码信息" + passwordShare);
        }

        return ControllerResult.toReturnDataSuccess("", passwordShare);
    }

    /**
     * @Description: 根据deviceMac查询密码信息(基础信息)
     * @param: deviceMac
     * @param: type
     * @return: com.zhuoyue.common.dto.ControllerResult
     * @author:wjming
     * @Date:2021/4/17 13:47
     */
    @GetMapping("/base/message")
    public ControllerResult returnLocalPassword(String deviceMac, String type) {


        TblPasswordShare passwordShare = tblPasswordShareMapper.selectOne(Wrappers.<TblPasswordShare>lambdaQuery()
                .eq(TblPasswordShare::getDeviceMac, deviceMac)
                .eq(TblPasswordShare::getType, type));
        if (passwordShare == null) {
            return ControllerResult.toNoDataFail("数据不存在");
        }
        TblPasswordShareDTO tblPasswordShareDTO = new TblPasswordShareDTO();
        BeanUtils.copyProperties(passwordShare, tblPasswordShareDTO);
        tblPasswordShareDTO.setBeginTime(Time.toLongMilli(passwordShare.getBeginTime()));
        tblPasswordShareDTO.setEndTime(Time.toLongMilli(passwordShare.getEndTime()));
        return ControllerResult.toReturnDataSuccess("获取成功", tblPasswordShareDTO);
    }


}
